package com.eascs.admin.o2o.receivedRecords.dao;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.o2o.receivedRecords.entity.ReceivedRecordsVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;

@Repository
public class ReceivedRecordsDao extends BaseO2oDao {
	/**
	 * 分页查询--回款记录
	 * 
	 * @param vo
	 * @return
	 */
	public PageResult<Object[]> getHKJLByPage(ReceivedRecordsVo vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		// -----------------------id-----loanapply_no--------贷款单号--------省区---
		querySql.append(" select orr.id,oll.loanapply_no,oll.bill_code,opr.province_name, ");////
		// ----------------信贷经理-------------借款人姓名-----------------
		querySql.append(" oll.operator_name,olu.customer_name as loaner_name, ");
		// ----------------------放款银行---------------------------------------------------------------------
		querySql.append(" ppd.item_name as loan_bank, ");
		// -------------放款金额-----回款总额(待改,实际是单次回款金额)------回款日期
		querySql.append(" obld.loan_amount,orr.received_amount,orr.received_date ");
		querySql.append(" from o2o_received_records orr "
				+ " left join o2o_loanapply oll on oll.loanapply_no = orr.loanapply_no "
				+ " left JOIN o2o_loan_user olu on oll.loanapply_no = olu.loanapply_no "
				+ " left JOIN o2o_bank_loan_data obld on oll.bill_code = obld.bill_code "
				+ " left JOIN o2o_platform_regional opr on oll.ltd = opr.platform_id "
				+ " left join pub_dditem ppd on obld.loan_bank=ppd.item_code "
				+ " left join pub_datadictionary pdt on ppd.dd_no = pdt.dd_no ");
		querySql.append(
				" where 1=1 and /**DATA_PERMISSION(oll.ltd)**/ olu.spouse_id is null and olu.sponsor_type is null and pdt.dd_code = 'BANKTYPEO2O' and orr.is_del='N' ");// 付款类型'ZD'是什么
		if (!StringUtil.isEmpty(vo.getBillCode())) {
			querySql.append(" and oll.bill_code like ? ");
			sqlPageQuery.addLikeParam(vo.getBillCode());
		}
		if (!StringUtil.isEmpty(vo.getLoanBank())) {
			querySql.append(" and loan_bank = ? ");
			sqlPageQuery.addParam(vo.getLoanBank());
		}
		if (!StringUtil.isEmpty(vo.getOperatorName())) {// 信贷经理
			querySql.append(" and oll.operator_name like ? ");
			sqlPageQuery.addLikeParam(vo.getOperatorName());
		}
		if (!StringUtil.isEmpty(vo.getLoanerName())) {
			querySql.append(" and loaner_name like ? ");
			sqlPageQuery.addLikeParam(vo.getLoanerName());
		}
		if (!StringUtil.isEmpty(vo.getProvinceCode())) {
			querySql.append(" and opr.province_code = ? ");
			sqlPageQuery.addParam(vo.getProvinceCode());
		}

		querySql.append(" order by orr.received_date desc ");
		// querySql.append(" order by orr.create_date desc ");
		sqlPageQuery.setSumColumnIndexs(new int[] { 8 });//放款金额统计、数据统计
		sqlPageQuery.setQuerySql(querySql.toString());
		return this.findDataWithPage(sqlPageQuery);
	}

	/**
	 * 新增回款记录--根据贷款单号查询单据基本信息
	 * 
	 * @param billCode
	 * @return
	 */
	public ReceivedRecordsVo findReceivedRecordsEntity(String billCode) {
		StringBuffer querySql = new StringBuffer();
		querySql.append(
				"select  oll.loanapply_no AS loanapplyNo,oll.bill_code AS billCode,olu.customer_name AS loanerName,oll.operator_no AS operatorNo,oll.operator_name AS operatorName,opr.province_code AS provinceCode,opr.province_name AS provinceName,obld.loan_amount AS loanAmount,ppd.item_name AS loanBank  ");
		querySql.append(" from o2o_loanapply oll inner JOIN o2o_loan_user olu on oll.loanapply_no = olu.loanapply_no ");
		querySql.append(" left JOIN o2o_platform_regional opr on oll.ltd = opr.platform_id ");
		querySql.append(" left JOIN o2o_bank_loan_data obld on oll.bill_code = obld.bill_code ");
		querySql.append(" left JOIN pub_dditem ppd ON obld.loan_bank = ppd.item_code ");
		querySql.append(" left JOIN pub_datadictionary pdt ON ppd.dd_no = pdt.dd_no ");
		querySql.append(" where olu.spouse_id is NULL and olu.sponsor_type is null AND pdt.dd_code = 'BANKTYPEO2O' and oll.islock = 'N' and oll.wf_type != 'T' ");
		querySql.append(" and oll.bill_code =? ");
		return this.findEntity(querySql.toString(), ReceivedRecordsVo.class, billCode);
	}

	public <T> int insertRecords(T receivedEntity) {
		return o2oJdbcSession.insert(receivedEntity);
	}

}
